package com.company.restaurant;


import com.company.exception.InitializationException;
import com.company.exception.StarterException;
import com.company.restaurant.service.NetworkManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;

/**
 * Created by User on 12.02.14.
 */
public class HallServer implements Server, ServerLifeCycle {
    private static final Logger logger = LoggerFactory.getLogger(HallServer.class);

    private ServerConfiguration config;
    private ApplicationContext context;

    public HallServer(ServerConfiguration config) {
        this.config = config;
    }

    @Override
    public synchronized void init(ServerConfiguration config) throws InitializationException {
        initServerComponents();
    }

    @Override
    public synchronized void start() throws StarterException {

        try {
            wait();
        } catch (InterruptedException e) {
            logger.error("Interrupted", e);
            stop();
        }
    }

    @Override
    public synchronized void stop() {

    }

    @Override
    public void initServerComponents() throws InitializationException{
        initContext();
        NetworkManager networkManager = (NetworkManager)context.getBean("networkManager");
        try {
            LocalSessionFactoryBean sessionFactory = (LocalSessionFactoryBean) context.getBean("&sessionFactory");
        }catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    @Override
    public ApplicationContext getContext() {
        return context;
    }

    private void initContext() {
        try {
            context = new ClassPathXmlApplicationContext("spring-config.xml");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
}
